﻿Ext.namespace('FantastPlatform', 'FantastPlatform.UI');
FantastPlatform.UI.PatrolTaskGrid = function (config) {

    Ext.apply(this, config);

    var fileStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/PatrolTaskViewService.aspx?Action=Search'

        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Id',
            fields: ['Id', 'DutyOrganization', 'TaskType', 'DefaultLeader', 'BeginTime', 'EndTime', 'Require', 'Name', 'Description']
        }),
        remoteStore: true
    });

    this.store = fileStore;

    var paperBar = new FantastPlatform.Common.PapeToolBar({
        store: fileStore,
        noSort: true,
        defaultNum: 21
    });
    this.paperBar = paperBar;
    this.bbar = paperBar;
    this.pageNumber = paperBar.pageSize;

    var nm = new Ext.grid.RowNumberer();
    var sm = new Ext.grid.CheckboxSelectionModel();
    function renderLast(value, p, record) {

    };
    this.columns = [nm, sm, {
        header: 'Id',
        dataIndex: 'Id',
        hidden: true
    }, {
        header: '指令名称',
        dataIndex: 'Name',
        sortable: true
    }, {
        header: '牵头单位',
        dataIndex: 'DutyOrganization',
        sortable: true
    }, {
        header: '责任领导',
        dataIndex: 'DefaultLeader',
        sortable: true
    }, {
        header: '开始时间',
        dataIndex: 'BeginTime',
        sortable: true
    },
   {
       header: '结束时间',
       dataIndex: 'EndTime',
       sortable: true
   }
]

    FantastPlatform.UI.PatrolTaskGrid.superclass.constructor.call(this, {
        loadMask: { msg: '正在加载数据...' },
        sm: sm
    });
}

Ext.extend(FantastPlatform.UI.PatrolTaskGrid, Ext.grid.GridPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.PatrolTaskGrid.superclass.onRender.call(this, ct, position);
    },

    //添加
    onAddPatrolTaskClick: function () {
        var id = GUID();
        currtWind = openWind("../PatrolTaskPanel.aspx", id, '新增调警指令', 800, 550, 'new_tab')
    },

    //修改
    onEditPatrolTaskClick: function () {
        var node = checkGridSelected(this);
        if (node) {
            if (node.length > 1) {
                showErrorMessage("操作失败", "只能选择一条数据进行编辑", "");
            } else {
                var fId = node[0].get('Id');
                var id = GUID();
                currtWind = openWind('../PatrolTaskPanel.aspx?id=' + fId, id, '编辑调警指令', 800, 550, 'edit');
            }
        }
    },

    onOpenAttendanceWinClick: function () {
        var node = checkGridSelected(this);
        if (node) {
            if (node.length > 1) {
                showErrorMessage("操作失败", "只能选择一条数据进行查看", "");
            } else {
                var fId = node[0].get('Id');
                var id = GUID();
                currtWind = openWind('../ScheduleOfAttendanceGrid.aspx?Id=' + fId, id, '查看考勤情况', 720, 450, 'new_tab');
            }
        }
    },

    //删除
    onDeletePatrolTaskClick: function () {
        var grid = this;
        var node = checkGridSelected(this);
        if (node) {
            var flag = true;
            if (node.length > 1) {
                var names = getIdFromGrid(node, 'CreatePoliceManNumber').split(',');
                var i = 0;
                for (i; i < names.length; i++) {
                    if (names[i] != policemanName) {
                        flag = false;
                        break;
                    }
                }
            }
            else {
                if (getIdFromGrid(node, 'CreatePoliceManNumber') != policemanNumber) {
                    flag = false;
                }
            }
            if (!flag) {
                showErrorMessage('操作失败', '只有该计划的创建人才有权限删除');
            } else {
                Ext.Msg.confirm('提示', '确认要删除选中的数据吗？', function (btn) {
                    if (btn == 'yes') {
                        Ext.Msg.wait('正在删除数据，请稍候……', '删除数据');
                        var fIds = getIdFromGrid(node, 'Id');
                        ajaxCalled(null, '../Service/WorkSchedule.aspx?Action=Delete', fIds, grid, null);
                    }

                })
            }
        }
    },
    //复制
    onCopyPatrolTaskClick: function () {
        var node = checkGridSelected(this);
        if (node) {
            if (node.length > 1) {
                showErrorMessage("操作失败", "只能选择一条数据进行编辑", "");
            } else {
                var fId = node[0].get('Id');
                var id = GUID();
                currtWind = openWind('../PatrolTaskCopyPanel.aspx?id=' + fId, id, '复制调警指令', 800, 550, 'copy');
            }
        }
    },

    //查询
    onSearchClick: function () {
        var id = GUID();
        currtWind = openWind('../WorkScheduleSearchForm.aspx', id, '查询', 360, 170, 'new_tab');
    },

    //刷新
    onUpdataClick: function () {
        this.store.removeAll();
        this.store.proxy.conn = { method: 'post', url: this.url };
        this.store.load({ params: { start: 0, limit: this.paperBar.getPageNumber(), dir: '', sort: ''} });
    }
});